using System;
using System.Collections.Generic;
using UnityEngine;

namespace ai_library
{
	public class OpportunityException : System.Exception
	{
		protected string message;
		public OpportunityException(string message) { this.message = message; }
		public override string Message { get { return message; } }
		public override string ToString() { return "Opportunity Exception: " + message; }
	}

	public class Opportunity<T> : IOpportunity<T> where T : IEntity
	{
		private ICondition<T> m_condition;

		public Opportunity(ICondition<T> condition, List<IAction<T>> actions)
		{
			Actions = actions;
			m_condition = condition;
		}

		public List<IAction<T>> Actions
		{
			get;
			private set;
		}

		public bool IsSatisfied(T t)
		{
			try
			{
				if (m_condition.Condition(t))
				{
					foreach(IAction<T> action in Actions)
					{
						action.Action(t);
					}

					return true;
				}
			}
			catch(ConditionException e)
			{
				throw new OpportunityException(e.ToString());
			}
			catch(ActionException e)
			{
				throw new OpportunityException(e.ToString());
			}

			return false;
		}
	}
}
